﻿using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Mt.Site.Core.ContentManagement.Entities;
using Mt.Site.Core.Navigations;
using System;
using System.Threading.Tasks;

namespace Mt.Site.Core.DataLogs
{
    public class DataLogsManager : DomainService, IDataLogsManager
    {
        private IRepository<DataLog, Guid> _datalogRepositor;                                 // 公共仓储
        private readonly IMenuStore _menuStore;

        /// <summary>
        /// 自动注入仓储
        /// </summary>
        /// <param name="datalogRepositor"></param>
        public DataLogsManager(IMenuStore menustore, IRepository<DataLog, Guid> datalogRepositor)
        {
            _menuStore = menustore;
            _datalogRepositor = datalogRepositor;
        }

        /// <summary>
        /// 删除记录信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(Guid Id)
        {
            await _datalogRepositor.DeleteAsync(Id);
        }

        public async Task CreateAsync(Content content, DataLogOptionType dataLogOptionType)
        {
            var menuItem = await _menuStore.GetMenuIdTraceCacheItemAsync(content.MenuId);

            var datalogs = new DataLog
            {
                DataId = content.Id,
                DataAuxiliaryId = content.AuxiliaryId,
                DataTitle = content.Title,
                OptionType = dataLogOptionType,
                DataMenuName = menuItem.DisplayName(),
                MenuId = content.MenuId,
                EntityTypeName = content.GetType().Name
            };
            await _datalogRepositor.InsertAsync(datalogs);
        }

        public async Task CreateAsync(Content2 content, long menuId, DataLogOptionType dataLogOptionType)
        {
            var menuItem = await _menuStore.GetMenuIdTraceCacheItemAsync(menuId);

            var datalogs = new DataLog
            {
                DataId = content.Id,
                DataAuxiliaryId = content.AuxiliaryId,
                DataTitle = content.Title,
                OptionType = dataLogOptionType,
                DataMenuName = menuItem.DisplayName(),
                MenuId = menuId,
                EntityTypeName = content.GetType().Name
            };
            await _datalogRepositor.InsertAsync(datalogs);
        }
    }
}
